package wx

import (
	"xdkaika/libs/redis"

	beego "github.com/beego/beego/v2/server/web"
	"github.com/silenceper/wechat/v2"
	"github.com/silenceper/wechat/v2/miniprogram"
	miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
)

type config struct {
	AppID     string
	AppSecret string
	MchID     string
	Key       string
	NotifyURL string
}

func GetConfig() (cfg *config) {
	cfg = &config{
		AppID:     beego.AppConfig.DefaultString("appid", "appid"),
		AppSecret: beego.AppConfig.DefaultString("appsecret", "appsecret"),
		MchID:     beego.AppConfig.DefaultString("mchid", "mchid"),
		Key:       beego.AppConfig.DefaultString("mchkey", "mchkey"),
		NotifyURL: beego.AppConfig.DefaultString("notifyurl", "notifyurl"),
	}
	return
}

/**
*获取小程序示例
 */
func GetMiniProgram() *miniprogram.MiniProgram {
	wx := wechat.NewWechat()
	cache := redis.GetRedis()
	config := GetConfig()
	cfg := &miniConfig.Config{
		AppID:     config.AppID,
		AppSecret: config.AppSecret,
		Cache:     cache,
	}
	return wx.GetMiniProgram(cfg)
}
